﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Reflection;

namespace Sita.Entities.MetaData.CustomAttributes
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    public class LocalizedDisplayNameAttribute : DisplayNameAttribute
    {
        private PropertyInfo m_PropertyName;
        private Type m_ResourceType;

        public LocalizedDisplayNameAttribute(string displayName)
            : base(displayName)
        {
        }

        public Type ResourceType
        {
            get { return m_ResourceType; }
            set
            {
                m_ResourceType = value;
                m_PropertyName = m_ResourceType.GetProperty(base.DisplayName, BindingFlags.Static | BindingFlags.Public);
            }
        }

        public override string DisplayName
        {
            get
            {
                if (m_PropertyName == null)
                {
                    return base.DisplayName;
                }

                return (string)m_PropertyName.GetValue(m_PropertyName.DeclaringType, null);
            }
        }


    }
}